//
//  HMAddImageViewCompone.swift
//  swift微博项目
//
//  Created by huodian on 16/10/7.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

let margincell:CGFloat = 4

let  itemCount = 3


let collectionItemW = (screenWidth - CGFloat(itemCount + 1 ) * margincell )/CGFloat(itemCount)


let maxImage = 4





class HMAddImageViewCompone: UICollectionViewController {

    lazy var imageArray:[UIImage] = [UIImage]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView?.backgroundColor = UIColor.white

     
        self.collectionView!.register(HMCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

     
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }



    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     
        
        
           let  imagecount = self.imageArray.count
        
           let count = imagecount + (imageArray.count == maxImage ? 0 : 1)
        
        
        
        return count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMCollectionViewCell
    
       // cell.backgroundColor = random()
        if imageArray.count == indexPath.item {
            
            cell.image = nil
            
        }else
        {
            cell.image = imageArray[indexPath.item]

        }
        
        
        cell.delegate = self
    
        return cell
    }

}

protocol HMCollectionViewCellDelegate:NSObjectProtocol {
    
    func userAddImage()
    
    func userRemoveImage(cell:HMCollectionViewCell)
    
}

extension HMAddImageViewCompone:HMCollectionViewCellDelegate,UINavigationControllerDelegate{
    internal func userRemoveImage(cell: HMCollectionViewCell) {
        
        let indexPath = collectionView?.indexPath(for: cell)
        
        imageArray.remove(at: indexPath!.item)
        //在collectionViewController中不能用View代替collectionView
        self.collectionView!.isHidden = imageArray.count == 0
        
        self.collectionView?.reloadData()
        
        
        
    }

  

    internal func userAddImage() {
        
        
        let loadImage = UIImagePickerController()
        
        loadImage.delegate = self
        
        present(loadImage, animated: true, completion: nil)
        
     
    }

    
    
    
    
    
    
    
}

extension HMAddImageViewCompone:UIImagePickerControllerDelegate{
    
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        
        imageArray.append(image.scaleImage(width: 600))
        
        self.collectionView?.reloadData()
        
        dismiss(animated: true, completion: nil)
        
//        if imageArray.count != 0 {
//            
//          self.collectionView?.isHidden = false
//        }
        
  
    }
    
    
    
}

class HMCollectionViewCell: UICollectionViewCell {
    
    
    var delegate:HMCollectionViewCellDelegate?
    
    var image:UIImage?{
        
        didSet{
            
            addTheBtn.setImage(image, for: UIControlState.normal)
            
            removeBtn.isHidden = (image == nil ? true : false)
            
            let backImage:UIImage? = (image == nil ? #imageLiteral(resourceName: "compose_pic_add") : nil)
            
            addTheBtn.setBackgroundImage(backImage, for: UIControlState.normal)
            
            
            
            
        }
        
    }
    
    
    
    override init(frame: CGRect) {
       
        super.init(frame: frame)
    
        
        
        setMyUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setMyUI(){
        
        self.contentView.addSubview(addTheBtn)
        
        self.contentView.addSubview(removeBtn)
        
        addTheBtn.snp.makeConstraints { (make) in
            
            make.edges.equalTo(self.contentView)
            
        }
        
        removeBtn.snp.makeConstraints { (make) in
            make.trailing.top.equalTo(self.contentView)
        }
        
        addTheBtn.addTarget(self, action: #selector(clickAddTheBtn), for: UIControlEvents.touchUpInside)
        
        removeBtn.addTarget(self, action: #selector(clickRemoveBtn), for: UIControlEvents.touchUpInside)
        
        
        
    }
  
    func clickAddTheBtn(){
        
        if image != nil {
            
            
            return
        }
        
        delegate?.userAddImage()
        
    }
    
    func clickRemoveBtn(){
        
        delegate?.userRemoveImage(cell:self)
        
    }
    
    lazy var addTheBtn:UIButton = {
       
        let btn = UIButton()
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "compose_pic_add"), for: UIControlState.normal)

        btn.setBackgroundImage(#imageLiteral(resourceName: "compose_pic_add_highlighted"), for: UIControlState.highlighted)
        
        return btn
    }()
    
    lazy var removeBtn:UIButton = {
        
        let btn  = UIButton()
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "compose_photo_close"), for: UIControlState.normal)
        
        
        
        return btn
    
    }()
}
